package confidence.helpers;


public class Vector3 {
	protected double scale;
	public double x, y, z;

	public Vector3(double pX, double pY, double pZ, double pScale) {
		x = pX;
		y = pY;
		z = pZ;
		scale = pScale;
	}

	public Vector3(double pX, double pY, double pZ) {
		this(pX, pY, pZ, 1);
	}

	public Vector3 add(Vector3 pVector) {
		return new Vector3(x + pVector.x, y + pVector.y, z + pVector.z);
	}

	public Vector3 add(double pScalar) {
		return new Vector3(x + pScalar, y + pScalar, z + pScalar);
	}

	public Vector3 subtract(Vector3 pVector) {
		return new Vector3(x - pVector.x, y - pVector.y, z - pVector.z);
	}

	public Vector3 subtract(double pScalar) {
		return add(-1 * pScalar);
	}


	public Vector3 multiply(double pScalar) {
		return new Vector3(x * pScalar, y * pScalar, z * pScalar);
	}

	public Vector3 divide(double pScalar) {
		return new Vector3(x / pScalar, y / pScalar, z / pScalar);
	}

	public double dot(Vector3 pVector) {
		return x * pVector.x + y * pVector.y + z * pVector.z;
	}

	public double dot4(Vector3 pVector) {
		return x * pVector.x + y * pVector.y + z * pVector.z + scale * pVector.scale;
	}

	public Vector3 cross(Vector3 pVector) {
		return new Vector3(y * pVector.z - z * pVector.y, z * pVector.x - x * pVector.z, x
				* pVector.y - y * pVector.x);
	}
	
	public boolean equals (Vector3 pVector) {
		if (this.scale==pVector.scale && this.x==pVector.x && this.y==pVector.y && this.z==pVector.z) {
			return true;
		}
		else {
			return false;
		}
	}

	public double length() {
		return Math.sqrt(x * x + y * y + z * z);
	}

	public Vector3 toLength1() {
		return divide(length());
	}

	public double phi() {
		return Math.atan2(y, x);
	}

	public double theta() {
		return Math.atan2 (z, Math.sqrt(x * x + y * y));
	}

	public int nanCount() {
		int result = 0;

		if (Double.isNaN(x)) {
			result++;
		}
		if (Double.isNaN(y)) {
			result++;
		}
		if (Double.isNaN(z)) {
			result++;
		}
		return result;
	}

	public String toString() {
		return "(" + x + ", " + y + ", " + z + ")";
	}
	
	public String toTabString() {
		return x + "\t" + y + "\t" + z;
	}
}
